/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package station;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.MulticastSocket;

/**
 *
 * @author safe
 */
public class MulticastListener extends Thread {

    private MulticastSocket multicastSocket;
    private final int messsageLength;

    protected MulticastListener(MulticastSocket multicastSocket, int messsageLength) {
        this.multicastSocket = multicastSocket;
        this.messsageLength = messsageLength;
    }

//    private long pushNewMessageWithTimestamp(byte[] message) {
//        long timestamp = System.currentTimeMillis() +  DataAbweichungsZeit.getAbWeichungsZeit();
//        DataEmpfangeneNachrichten.putIntoMessageMap(timestamp, message);
//        return timestamp;
//    }
    @Override
    public void run() {

        byte[] byteArray = new byte[messsageLength];
        DatagramPacket datagram = new DatagramPacket(byteArray, byteArray.length);
        long timestamp;
        while (!this.isInterrupted()) {

            try {
                multicastSocket.receive(datagram);
//                timestamp = this.pushNewMessageWithTimestamp(byteArray);
                timestamp = System.currentTimeMillis() + DataAbweichungsZeit.getAbWeichungsZeit();
                StationUpdateWorker worker = new StationUpdateWorker(timestamp, byteArray);
                worker.start();
            } catch (IOException ex) {
//                Logger.getLogger(MulticastListener.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
